package 练习;

class NewThread implements Runnable {
    Thread t;
    NewThread() {
        // Create a new, second thread
        t = new Thread(this, "Demo Thread");
        System.out.println("Child thread: " + t);
        t.start(); // Start the thread
    }
    // This is the entry point for the second thread.
    public void run() {
        try {
            for(int i = 5; i > 0; i--) {
                System.out.println("Child Thread: " + i);
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            System.out.println("Child interrupted.");
        }
        System.out.println("Exiting child thread.");
    }
}


class ThreadDemo {
    public static void main(String args[]) throws InterruptedException {
        Thread t = new Thread(() -> {
            try {
                for(int i = 5; i > 0; i--) {
                    System.out.println("Child Thread: " + i);
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                System.out.println("Child interrupted.");
            }
            System.out.println("Exiting child thread.");
        });
//        t.setPriority(11);
        t.start();
        Thread t2 = new Thread(() -> {
            try {
                for(int i = 5; i > 0; i--) {
                    System.out.println("Child Thread2: " + i);
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                System.out.println("Child interrupted.");
            }
            System.out.println("Exiting child thread.");
        });
//        t2.setPriority(15);
        t2.start();
//        t.join();
//        t2.join();
        try {
            for(int i = 5; i > 0; i--) {
                System.out.println("Main Thread: " + i);
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            System.out.println("Main thread interrupted.");
        }
        System.out.println("Main thread exiting.");
    }
}